﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace Avatar.BLL
{
    public class Search_Js
    {
        #region 私有变量
        /// <summary>
        /// 职业js
        /// </summary>
        private static string _getposition_jsFile;
        /// <summary>
        /// 省份js
        /// </summary>
        private static string _getProvince_jsFile;
        static StreamWriter sw;
        #endregion

        #region 属性
        /// <summary>
        /// 职业js
        /// </summary>
        public static string getPosition_JSFile
        {
            set { _getposition_jsFile = value; }
        }
        /// <summary>
        /// 省份js
        /// </summary>
        public static string getProvince_JSFile
        {
            set { _getProvince_jsFile = value; }
        }
        public Search_Js()
        {
            //Command.sqlCommand();
        }
        #endregion

        /// <summary>
        /// 通过参数返回一个DataTable结果集
        /// </summary>
        /// <param name="DataBase_Name">数据表名</param>
        /// <returns>DataTable</returns>
        private static DataTable DataBase(string DataBase_Name)
        {
            string ConnString = ConfigurationManager.ConnectionStrings["0745Job"].ToString();
            SqlConnection Conn = new SqlConnection(ConnString);
            string sql = "select * from " + DataBase_Name + " order by orderID desc, id asc";
            SqlDataAdapter da = new SqlDataAdapter(sql, Conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        /// <summary>
        /// 生成职业js
        /// </summary>
        public static bool WriteJS_Position()
        {
            sw = new StreamWriter(_getposition_jsFile, false, Encoding.GetEncoding("UTF-8"));
            DataTable dt_JobTree = DataBase("JobTree");
            sw.WriteLine("positionarray = new Array();");
            for (int i = 0; i < dt_JobTree.Rows.Count; i++) {
                sw.WriteLine("positionarray[" + i + "] = new Array(\"" + dt_JobTree.Rows[i][2] + "\", \"" + dt_JobTree.Rows[i][1] + "\", \"" + dt_JobTree.Rows[i][0] + "\");");
            }
            sw.Flush();
            sw.Close();
            return true;
        }

        /// <summary>
        /// 生成地区js
        /// </summary>
        public static bool WriteJS_Province()
        {
            sw = new StreamWriter(_getProvince_jsFile, false, Encoding.GetEncoding("UTF-8"));
            DataTable dt_JobTree = DataBase("FromTree");
            sw.WriteLine("provincearray = new Array();");
            for (int i = 0; i < dt_JobTree.Rows.Count; i++) {
                sw.WriteLine("provincearray[" + i + "] = new Array(\"" + dt_JobTree.Rows[i][2] + "\", \"" + dt_JobTree.Rows[i][1] + "\", \"" + dt_JobTree.Rows[i][0] + "\");");
            }
            sw.Flush();
            sw.Close();
            return true;
        }
    }
}
